package com.shark.erp.controller;

import cn.hutool.core.text.csv.CsvUtil;
import com.alibaba.fastjson.JSONObject;
import com.shark.erp.entity.Customer;
import com.shark.erp.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @ResponseBody
    @GetMapping("/selectAllCustomer")
    public JSONObject selectAllCustomer() {
        return customerService.selectAllCustomer();
    }

    @ResponseBody
    @GetMapping("/selectCustomerByName")
    public JSONObject selectCustomerByName(@RequestParam("name") String name) {
        return customerService.selectCustomerByName(name);
    }

    @ResponseBody
    @PostMapping("/insertCustomer") //获取前端传来的json参数
    public JSONObject insertCustomer(@RequestBody Customer customer) {
        return customerService.insertCustomer(customer);
    }

    @ResponseBody
    @DeleteMapping("/deleteCustomer")
    public JSONObject deleteCustomer(@RequestParam("code") String code) {
        return customerService.deleteCustomer(code);
    }

    @ResponseBody
    @PostMapping("/updateCustomer")
    public JSONObject updateCustomer(@RequestBody Customer customer) {
        return customerService.updateCustomer(customer);
    }

    /**
     * 区分该表单请求是添加还是修改
     */
    @PostMapping("/customerAction")
    public String customerAction(@RequestParam("action") String action) {
        if (action.equals("edit")) {
            return "forward:/updateCustomer";
        } else if (action.equals("add")) {
            return "forward:/insertCustomer";
        }
        return "404";
    }

    /**

     * 下载Excel文件

     * @date 2021/11/4 21:12

     * @author zk_yjl

     * @return

     */

    @GetMapping("/customer/downloadCsv")

    @ResponseBody

    public void downloadCsv(HttpServletResponse response) throws IOException {

        //1. 通过传入的参数，和相关的业务代码逻辑处理，获取相应的数据.

        //2. 将数据进行转换，转换成 List<User> 的形式.
        JSONObject jsonObject = customerService.selectAllCustomer();
        List<Customer> dataList=jsonObject.getObject("data",List.class);

        //将数据进行下载.

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman

        try {

            //响应类型

            response.setContentType("multipart/form-data");

            response.setCharacterEncoding("utf-8");

            //进行下载

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系

            String fileName = URLEncoder.encode("客户表", "UTF-8").replaceAll("\\+", "%20");

            //响应的是  .csv 文件的后缀

            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".csv");

            // 这里需要设置不关闭流

            String filePath= File.separator+fileName+".csv";

            File file=new File(filePath);

            if(!file.exists()){

                file.createNewFile();

            }

            //将数据，写入到 文件里面。 主要是这一行代码逻辑

            CsvUtil.getWriter(file, Charset.forName("UTF-8")).writeBeans(dataList).close();

            downloadFile(response,file);

            //将该文件删除

            file.delete();

        } catch (Exception e) {

            // 重置response

            response.reset();

            response.setContentType("application/json");

            response.setCharacterEncoding("utf-8");

            Map<String, String> map = new HashMap<String, String>();

            map.put("status", "failure");

            map.put("message", "下载文件失败" + e.getMessage());

             jsonObject=new JSONObject();;

            response.getWriter().println(jsonObject.toString());

        }

    }

    /**

     * @return boolean

     * @Description 下载文件

     * @Param response，file

     **/

    public boolean downloadFile(HttpServletResponse response, File file) {

        FileInputStream fileInputStream = null;

        BufferedInputStream bufferedInputStream = null;

        OutputStream os = null;

        try {

            fileInputStream = new FileInputStream(file);

            bufferedInputStream = new BufferedInputStream(fileInputStream);

            os = response.getOutputStream();

            //MS产本头部需要插入BOM

            //如果不写入这几个字节，会导致用Excel打开时，中文显示乱码

            os.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});

            byte[] buffer = new byte[1024];

            int i = bufferedInputStream.read(buffer);

            while (i != -1) {

                os.write(buffer, 0, i);

                i = bufferedInputStream.read(buffer);

            }

            return true;

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            //关闭流

            if (os != null) {

                try {

                    os.flush();

                    os.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if (bufferedInputStream != null) {

                try {

                    bufferedInputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if (fileInputStream != null) {

                try {

                    fileInputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            file.delete();

        }

        return false;

    }
}
